/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ebdesk.controller.dhkp;
import com.ebdesk.view.dhkp.DHKPInternalFrame;
import com.ebdesk.controller.dhkp.NumberToText;
import com.ebdesk.view.MainFrame;
import com.ebdesk.view.dhkp.PrintDialog;
import com.ebdesk.controller.dhkp.Lib;
import com.ebdesk.view.MainFrame;
import com.java4less.textprinter.ports.*;
import com.java4less.textprinter.*;
import java.awt.*;
import java.util.*;
import java.util.logging.*;
import java.text.*;
import javax.swing.*;
import java.util.List;

/**
 *
 * @author mbenk
 */
public class Print{
    private final static int WIDTHRES = 175;
    private final static int KOLRES = 66;
    private final static int KOLHAL = 150;
    private final static int ROWSPERPAGE = 15;
    
   
    private FilePort fp;
    private TextPrinter tp;
    private JobProperties jp;
    private TextProperties p;
  
    private int baris;
    private int kolom;
    
    private int noHal;
    private int jmlHal;    
    private int[] array_column = {2, 8, 19, 35, 70, 105, 126, 150};
    private int firstRow;  
    private int blocklength;

    
    private double []TotalSHI;

    private double totalHI = 0;
    private int rowFooter = 8;
    
    private DHKP dhkp;
    
    private String tanggalTerbit,tanggalPenetapan;
    private String namaPejabat,nipPejabat;
    private String namaProvinsi;
    private String namaKota;

    public Print(){
//        dhkp = new DHKP();
    }
    public Print(String printFile){
        try{
            dhkp = new DHKP();
            this.fp = new FilePort (printFile);
            this.tp = PrinterFactory.getPrinter("PLAIN"); 
            namaProvinsi = dhkp.getQuery("select nama_provinsi from pbb.pbb_master_propinsi where kode_provinsi="
                                        + "(select pbb.pbb_setting.value from pbb.pbb_setting where pbb.pbb_setting.setting = 'kode_provinsi')").toString();
            
            namaKota = dhkp.getQuery("select nama_kota from pbb.pbb_master_kota where kode_kota="
                                    + "(select pbb.pbb_setting.value from pbb.pbb_setting where pbb.pbb_setting.setting = 'kode_kota')").toString();
            
            tanggalTerbit = dhkp.getQuery("select pbb.pbb_setting.value from pbb.pbb_setting where pbb.pbb_setting.setting = 'tanggal_terbit'").toString();
            tanggalPenetapan = dhkp.getQuery("select pbb.pbb_setting.value from pbb.pbb_setting where pbb.pbb_setting.setting = 'tanggal_penetapan'").toString();
            namaPejabat = dhkp.getQuery("select pbb.pbb_setting.value from pbb.pbb_setting where pbb.pbb_setting.setting = 'nama_pejabat'").toString();
            nipPejabat = dhkp.getQuery("select pbb.pbb_setting.value from pbb.pbb_setting where pbb.pbb_setting.setting = 'nip_pejabat'").toString();
        }
        catch(Exception e){
        } 
    } 
    public void setBlockLength(int length){
        blocklength = length;
    }
    public void setPrint(String printFile){
         try{
            dhkp = new DHKP();
            this.fp = new FilePort (printFile);
            this.tp = PrinterFactory.getPrinter("PLAIN"); 
            
            namaProvinsi = dhkp.getQuery("select nama_provinsi from pbb.pbb_master_propinsi where kode_provinsi="
                                        + "(select pbb.pbb_setting.value from pbb.pbb_setting where pbb.pbb_setting.setting = 'kode_provinsi')").toString();
            
            namaKota = dhkp.getQuery("select nama_kota from pbb.pbb_master_kota where kode_kota="
                                    + "(select pbb.pbb_setting.value from pbb.pbb_setting where pbb.pbb_setting.setting = 'kode_kota')").toString();
            
            tanggalTerbit = dhkp.getQuery("select pbb.pbb_setting.value from pbb.pbb_setting where pbb.pbb_setting.setting = 'tanggal_terbit'").toString();
            tanggalPenetapan = dhkp.getQuery("select pbb.pbb_setting.value from pbb.pbb_setting where pbb.pbb_setting.setting = 'tanggal_penetapan'").toString();
            namaPejabat = dhkp.getQuery("select pbb.pbb_setting.value from pbb.pbb_setting where pbb.pbb_setting.setting = 'nama_pejabat'").toString();
            nipPejabat = dhkp.getQuery("select pbb.pbb_setting.value from pbb.pbb_setting where pbb.pbb_setting.setting = 'nip_pejabat'").toString();
        }
        catch(Exception e){
        } 
    }
    public double getTotalPajak(String data[][]){
        double pajak = 0;
        for(int i = 0; i < data.length; i++){
            pajak += Double.parseDouble(data[i][10]);
           
        }
        return pajak;
    }
    public double getLuasTanah(String data[][]){
        double lt = 0;
        for(int i = 0; i < data.length;i++){
            lt += Double.parseDouble(data[i][12]);
        }
        return lt;
    }
    public double getLuasBangunan(String data[][]){
        double lb = 0;
        for(int i = 0; i< data.length;i++){
            lb += Double.parseDouble(data[i][13]);
        }
        return lb;
    }
    public int getNoHalaman(String data[][]){
        int noHal = 1;
        int range = 15;
        for(int i = 0; i < data.length; i++){
            if((i % range)==0){
                noHal++;
            }
        }
        return noHal;
    }

    public void printFirstPage(MainFrame ud,String data[][]) throws ParseException{
        noHal = 1;
        jmlHal = getNoHalaman(data) + 1;
        tp.printString("Halaman "+noHal+" dari " + jmlHal, 0, KOLHAL, p);
        tp.printString(Lib.allignCenter("PEMERINTAH KOTA "+namaKota, WIDTHRES), 2, 0, p);
        tp.printString(Lib.allignCenter("DINAS PENDAPATAN DAERAH", WIDTHRES), 3, 0, p);
        tp.printString(Lib.allignCenter("KOTA "+namaKota,WIDTHRES),4,0,p);
        
        baris = 10;
        kolom = 57;
        
        tp.printString("DDDDDDDDDD", baris, kolom, p);
        tp.printString("DDDDDDDDDDD", baris + 1, kolom, p);
        tp.printString("DDD", baris + 2, kolom + 1, p);
        tp.printString("DDD", baris + 2, kolom + 9, p);
        tp.printString("DDD", baris + 3, kolom + 1, p);
        tp.printString("DDD", baris + 3, kolom + 9, p);
        tp.printString("DDD", baris + 4, kolom + 1, p);
        tp.printString("DDD", baris + 4, kolom + 9, p);
        tp.printString("DDD", baris + 5, kolom + 1, p);
        tp.printString("DDD", baris + 5, kolom + 9, p);
        tp.printString("DDD", baris + 6, kolom + 1, p);
        tp.printString("DDD", baris + 6, kolom + 9, p);
        tp.printString("DDD", baris + 7, kolom + 1, p);
        tp.printString("DDD", baris + 7, kolom + 9, p);
        tp.printString("DDD", baris + 8, kolom + 1, p);
        tp.printString("DDD", baris + 8, kolom + 9, p);
        tp.printString("DDD", baris + 9, kolom + 1, p);
        tp.printString("DDD", baris + 9, kolom + 9, p);
        tp.printString("DDDDDDDDDDD", baris + 10, kolom, p);
        tp.printString("DDDDDDDDDD", baris + 11, kolom, p);

        int colh = kolom + 15;
        tp.printString("HHH", baris, colh, p);
        tp.printString("HHH", baris, colh + 8, p);
        tp.printString("HHH", baris + 1, colh, p);
        tp.printString("HHH", baris + 1, colh + 8, p);
        tp.printString("HHH", baris + 2, colh, p);
        tp.printString("HHH", baris + 2, colh + 8, p);
        tp.printString("HHH", baris + 3, colh, p);
        tp.printString("HHH", baris + 3, colh + 8, p);
        tp.printString("HHH", baris + 4, colh, p);
        tp.printString("HHH", baris + 4, colh + 8, p);
        tp.printString("HHHHHHHHHHH", baris + 5, colh, p);
        tp.printString("HHHHHHHHHHH", baris + 6, colh, p);
        tp.printString("HHH", baris + 7, colh, p);
        tp.printString("HHH", baris + 7, colh + 8, p);
        tp.printString("HHH", baris + 8, colh, p);
        tp.printString("HHH", baris + 8, colh + 8, p);
        tp.printString("HHH", baris + 9, colh, p);
        tp.printString("HHH", baris + 9, colh + 8, p);
        tp.printString("HHH", baris + 10, colh, p);
        tp.printString("HHH", baris + 10, colh + 8, p);
        tp.printString("HHH", baris + 11, colh, p);
        tp.printString("HHH", baris + 11, colh + 8, p);

        int colk = colh + 15;
        tp.printString("KKK", baris, colk, p);
        tp.printString("KKK", baris, colk + 8, p);
        tp.printString("KKK", baris + 1, colk, p);
        tp.printString("KKK", baris + 1, colk + 7, p);
        tp.printString("KKK", baris + 2, colk, p);
        tp.printString("KKK", baris + 2, colk + 6, p);
        tp.printString("KKK", baris + 3, colk, p);
        tp.printString("KKK", baris + 3, colk + 5, p);
        tp.printString("KKK", baris + 4, colk, p);
        tp.printString("KKK", baris + 4, colk + 4, p);
        tp.printString("KKKKKK", baris + 5, colk, p);
        tp.printString("KKKKKK", baris + 6, colk, p);
        tp.printString("KKK", baris + 7, colk, p);
        tp.printString("KKK", baris + 7, colk + 4, p);
        tp.printString("KKK", baris + 8, colk, p);
        tp.printString("KKK", baris + 8, colk + 5, p);
        tp.printString("KKK", baris + 9, colk, p);
        tp.printString("KKK", baris + 9, colk + 6, p);
        tp.printString("KKK", baris + 10, colk, p);
        tp.printString("KKK", baris + 10, colk + 7, p);
        tp.printString("KKK", baris + 11, colk, p);
        tp.printString("KKK", baris + 11, colk + 8, p);

        int colp = colk + 15;
        tp.printString("PPPPPPPPPP", baris, colp, p);
        tp.printString("PPPPPPPPPPP", baris + 1, colp, p);
        tp.printString("PPP", baris + 2, colp + 1, p);
        tp.printString("PPP", baris + 2, colp + 9, p);
        tp.printString("PPP", baris + 3, colp + 1, p);
        tp.printString("PPP", baris + 3, colp + 9, p);
        tp.printString("PPP", baris + 4, colp + 1, p);
        tp.printString("PPP", baris + 4, colp + 9, p);
        tp.printString("PPPPPPPPPP", baris + 5, colp + 1, p);
        tp.printString("PPPPPPPPP", baris + 6, colp + 1, p);
        tp.printString("PPP", baris + 7, colp + 1, p);
        tp.printString("PPP", baris + 8, colp + 1, p);
        tp.printString("PPP", baris + 9, colp + 1, p);
        tp.printString("PPPPP", baris + 10, colp, p);
        tp.printString("PPPPP", baris + 11, colp, p);
        
        tp.printString(Lib.allignCenter("( DAFTAR HIMPUNAN KETETAPAN PAJAK DAN PEMBAYARAN )",WIDTHRES), baris+15, 0, p);
        tp.printString(Lib.allignCenter("( PAJAK BUMI DAN BANGUNAN )", WIDTHRES), baris+16, 0, p);
        
        tp.printString(Lib.allignCenter(ud.getKelompokBuku().toUpperCase(), WIDTHRES), baris + 17, 0, p);

        tp.printString(Lib.allignCenter("T A H U N  : "+ud.getTahun(), WIDTHRES), baris + 20, 0, p);
        
        Date terbit;
        DateFormat dateFormat;

        dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        terbit = (Date)dateFormat.parse(tanggalTerbit);
        tp.printString(Lib.allignCenter("TANGGAL TERBIT  : "+Lib.dateFormatIndo(terbit), WIDTHRES), baris + 23, 0, p);
        int colW = 60;
        
        String nop = ud.getNOP();
        
        tp.printString("PROPINSI", baris + 27, colW, p);
        tp.printString(":", baris + 27, colW + 22, p);
        tp.printString(nop.substring(0, 2), baris + 27, colW + 25, p);
        tp.printString("-", baris + 27, colW + 29, p);
        tp.printString(namaProvinsi, baris + 27, colW + 31, p);
     
        tp.printString("DATI II", baris + 28, colW, p);
        tp.printString(":", baris + 28, colW + 22, p);
        tp.printString(nop.substring(3, 5), baris + 28, colW + 25, p);
        tp.printString("-", baris + 28, colW + 29, p);
        tp.printString(namaKota, baris+28, colW+31, p);
        
        tp.printString("KECAMATAN", baris + 29, colW, p);
        tp.printString(":", baris + 29, colW + 22, p);
        tp.printString(ud.getKecamatan(),baris+29,colW + 25, p);
        
        tp.printString("KELURAHAN", baris + 30, colW, p);
        tp.printString(":", baris + 30, colW + 22, p);
        tp.printString(ud.getKelurahan(), baris + 30, colW + 25, p);
      
    }
    
    public void printHeader(MainFrame ud,String data[][],int baris,int i){
        String nop = ud.getNOP();
        jmlHal = getNoHalaman(data) + 1;
        
        tp.printString("Halaman "+i+" dari " + jmlHal, 0, KOLHAL, p);
        
        tp.printString(Lib.allignCenter("DAFTAR HIMPUNAN KETETAPAN PAJAK & PEMBAYARAN BUKU "+ud.getIndexBuku(), WIDTHRES), baris, 0, p);
        tp.printString(Lib.allignCenter("TAHUN   " + ud.getTahun(), WIDTHRES), baris + 1, 0, p);

        tp.printString("PROPINSI", baris + 3, 1, p);
        tp.printString(": " + nop.substring(0, 2) + " - " + namaProvinsi , baris + 3, 20, p);

        tp.printString("KECAMATAN", baris + 3, 120, p);
        tp.printString(": " + ud.getKecamatan(), baris + 3, 140, p);

        tp.printString("KOTA/KAB", baris + 4, 1, p);
        tp.printString(": " + nop.substring(3, 5) + " - " + namaKota, baris + 4, 20, p);

        tp.printString("KELURAHAN", baris + 4, 120, p);
        tp.printString(": " + ud.getKelurahan(), baris + 4, 140, p);
        tp.printHorizontalLine(baris + 5, 1, WIDTHRES);
        tp.printVerticalLine(array_column[0] - 1, baris + 5, baris + 8);

        tp.printString("NOMOR", baris + 6, array_column[0], p);
        tp.printVerticalLine(array_column[1] - 1, baris + 5, baris + 8);

        tp.printString(Lib.allignCenter("NOP", 11), baris + 6, array_column[1], p);
        tp.printVerticalLine(array_column[2] - 1, baris + 5, baris + 8);

        tp.printString(Lib.allignCenter("NOMOR", array_column[2]), baris + 6, array_column[2], p);
        tp.printString(Lib.allignCenter("INDUK", array_column[2]), baris + 7, array_column[2], p);

        tp.printVerticalLine(array_column[3] - 1, baris + 5, baris + 8);
        tp.printString(Lib.allignCenter("NAMA WAJIB PAJAK",array_column[4]-array_column[3]), baris + 6, array_column[3], p);
        tp.printVerticalLine(array_column[4] - 1, baris + 5, baris + 8);

        tp.printString(Lib.allignCenter("ALAMAT OBJEK PAJAK",array_column[4]-array_column[3]), baris + 6, array_column[4], p);
        tp.printString(Lib.allignCenter("WAJIB PAJAK",array_column[4]-array_column[3]), baris + 7, array_column[4], p);

        tp.printVerticalLine(array_column[5] - 1, baris + 5, baris + 8);
        tp.printString(Lib.allignCenter("PAJAK TERHUTANG", 20), baris + 6, array_column[5], p);
        tp.printVerticalLine(array_column[6] - 1, baris + 5, baris + 8);

        tp.printString(Lib.allignCenter("PERUBAHAN", array_column[6]-array_column[5]), baris + 6, array_column[6], p);
        tp.printString(Lib.allignCenter("PAJAK", array_column[6]-array_column[5]), baris + 7, array_column[6], p);

        tp.printVerticalLine(array_column[7] - 1, baris + 5, baris + 8);
        tp.printString(Lib.allignCenter("TANGGAL", array_column[7]-array_column[6]), baris + 6, array_column[7], p);
        tp.printString(Lib.allignCenter("BAYAR", array_column[7]-array_column[6]), baris + 7, array_column[7], p);

        tp.printVerticalLine(WIDTHRES - 1, baris + 5, baris + 8);
        tp.printHorizontalLine(baris + 8, 1, WIDTHRES);

    }
    public void printRow(MainFrame ud,String data[][],int j){
              
        tp.printVerticalLine(array_column[0] - 1, baris + 5, baris + 8);
        tp.printString(Lib.allignRight(Integer.toString(j+1), 6), baris + 6, array_column[0], p);
        tp.printVerticalLine(array_column[1] - 1, baris + 5, baris + 8);
        tp.printString(Lib.FormatSPPT(data[j][6]).substring(14, 24), baris + 6, array_column[1], p);
        tp.printVerticalLine(array_column[2] - 1, baris + 5, baris + 8);
        tp.printVerticalLine(array_column[3] - 1, baris + 5, baris + 8);
        tp.printString(data[j][7], baris + 6, array_column[3], p);
        tp.printVerticalLine(array_column[4] - 1, baris + 5, baris + 8);
        tp.printString(data[j][8], baris + 6, array_column[4], p);
        tp.printString(data[j][9], baris + 7, array_column[4], p);
        tp.printVerticalLine(array_column[5] - 1, baris + 5, baris + 8);
        tp.printString(Lib.allignRight(Lib.NumberFormat(Double.parseDouble(data[j][10])), 20), baris + 6, array_column[5], p);
      
        totalHI += Double.parseDouble(data[j][10]);
        tp.printVerticalLine(array_column[6] - 1, baris + 5, baris + 8);
        tp.printVerticalLine(array_column[7] - 1, baris + 5, baris + 8);
        tp.printHorizontalLine(baris + 8, 1, WIDTHRES);
        tp.printVerticalLine(WIDTHRES - 1, baris + 5, baris + 8);
           
    }
    public void printFooter(int row, int index){
       
        tp.printHorizontalLine(row, 1, WIDTHRES);
        tp.printVerticalLine(1, row, row + 3);
        tp.printVerticalLine(array_column[5] - 1, row, row + 3);
        tp.printString(Lib.allignRight(Lib.NumberFormat(totalHI), 20), row + 1, array_column[5], p);
        tp.printString(Lib.allignRight(Lib.NumberFormat(TotalSHI[index-2]), 20), row + 2, array_column[5], p);
        tp.printVerticalLine(array_column[6] - 1, row, row + 3);
        tp.printVerticalLine(WIDTHRES - 1, row, row + 3);
        tp.printString("Total Halaman Ini", row + 1, 5, p);
        tp.printString("Total Sampai Dengan Halaman Ini", row + 2, 5, p);
        tp.printHorizontalLine(row + 3, 1, WIDTHRES);
        totalHI = 0;
    }
    public void printLastPage(MainFrame ud,String data[][]) throws ParseException{
       
        int idxBuku;
        int i = 0;
        int jmlObjek = 0;
        double jmlluasBangunan = 0;
        double jmlluasTanah = 0;
        double jmlpokokKetetapan = 0;
        
        int itemCount = ud.getCountBuku()-1;
        
        int[] arrJmlObjek = new int[itemCount];
        double[] arrLuasBangunan = new double[itemCount];
        double[] arrLuasTanah = new double[itemCount];
        double[] arrPokokKetetapan = new double[itemCount];
        
        int row = 9;
        int colW = 60;
        
        jmlHal = getNoHalaman(data) + 1;
        noHal = jmlHal;
        String nop = ud.getNOP();
        
        tp.printString("Halaman "+noHal+" dari " + jmlHal, 0, KOLHAL, p);
        tp.printString(Lib.allignCenter("PEMERINTAH KOTA "+namaKota, WIDTHRES), 2, 0, p);
        tp.printString(Lib.allignCenter("DINAS PENDAPATAN DAERAH", WIDTHRES), 3, 0, p);
        tp.printString(Lib.allignCenter("KOTA "+namaKota,WIDTHRES),4,0,p);
        
        tp.printString("PROPINSI", row, colW, p);
        tp.printString(":", row, colW + 32, p);
        tp.printString(nop.substring(0, 2), row, colW + 35, p);
        tp.printString("-", row, colW + 39, p);
        tp.printString(namaProvinsi, row, colW + 41, p);

        tp.printString("DATI II", row + 1, colW, p);
        tp.printString(":", row + 1, colW + 32, p);
        tp.printString(nop.substring(3, 5), row + 1, colW + 35, p);
        tp.printString("-", row + 1, colW + 39, p);
        tp.printString(namaKota, row + 1, colW + 41, p);

        tp.printString("KECAMATAN", row + 2, colW, p);
        tp.printString(":", row + 2, colW + 32, p);
        tp.printString(ud.getKecamatan(), row + 2, colW + 35, p);

        tp.printString("KELURAHAN", row + 3, colW, p);
        tp.printString(":", row + 3, colW + 32, p);
        tp.printString(ud.getKelurahan(), row + 3, colW + 35, p);

        tp.printString(Lib.allignCenter("DAFTAR HIMPUNAN KETETAPAN PAJAK DAN PEMBAYARAN BUKU "+ud.getIndexBuku(), WIDTHRES), row + 7, 0, p);
        tp.printString(Lib.allignCenter("(  DHKP  )", WIDTHRES), row + 8, 0, p);
        tp.printString(Lib.allignCenter("PAJAK BUMI DAN BANGUNAN ", WIDTHRES), row + 9, 0, p);
        tp.printString(Lib.allignCenter("TAHUN : " + ud.getTahun(), WIDTHRES), row + 10, 0, p);

        tp.printString("DAFTAR INI TERDIRI ATAS", row + 14, colW, p);
        tp.printString(":", row + 14, colW + 32, p);
        tp.printString(Lib.NumberFormat(jmlHal), row + 14, colW + 35, p);
        tp.printString("HALAMAN", row + 14, colW + 45, p);

        tp.printString("JUMLAH SPPT SEBANYAK", row + 15, colW, p);
        tp.printString(":", row + 15, colW + 32, p);
        tp.printString(Lib.NumberFormat(data.length), row + 15, colW + 35, p);
        tp.printString(" LEMBAR", row + 15, colW + 45, p);
        
        int colrow = row + 17;

        int[] ar_col = new int[]{10+15, 20+15, 45+15, 70+15, 95+15, 120+15};

        // Header
        tp.printHorizontalLine(colrow, ar_col[0], ar_col[5] - 1);
        tp.printVerticalLine(ar_col[0] - 1, colrow, colrow + 2);
        tp.printString("Buku", colrow + 1, ar_col[0], p);
        tp.printVerticalLine(ar_col[1] - 1, colrow, colrow + 2);
        tp.printString(Lib.allignRight("Jumlah Objek", 25), colrow + 1, ar_col[1], p);
        tp.printVerticalLine(ar_col[2] - 1, colrow, colrow + 2);
        tp.printString(Lib.allignRight("Luas Bangunan", 25), colrow + 1, ar_col[2], p);
        tp.printVerticalLine(ar_col[3] - 1, colrow, colrow + 2);
        tp.printString(Lib.allignRight("Luas Tanah", 25), colrow + 1, ar_col[3], p);
        tp.printVerticalLine(ar_col[4] - 1, colrow, colrow + 2);
        tp.printString(Lib.allignRight("Pokok Ketetapan", 25), colrow + 1, ar_col[4], p);
        tp.printVerticalLine(ar_col[5] - 1, colrow, colrow + 2);
        tp.printHorizontalLine(colrow + 2, ar_col[0], ar_col[5] - 1);
        
        int colrowd = colrow + 3;
        
        idxBuku = ud.getIndexBuku();
        if(idxBuku == 0){
         //tidak melakukan apa-apa
        }
        else{
            while (i < itemCount){
                if(i == (idxBuku-1)){
                    arrJmlObjek[i] = data.length;
                    arrLuasBangunan[i] = getLuasBangunan(data);
                    arrLuasTanah[i] = getLuasTanah(data);
                    arrPokokKetetapan[i] = getTotalPajak(data);      
                }
                else{
                    arrJmlObjek[i] = 0;
                    arrLuasBangunan[i] = 0;
                    arrLuasTanah[i] = 0;
                    arrPokokKetetapan[i] = 0;      
                }
                tp.printVerticalLine(ar_col[0] - 1, colrowd, colrowd + 2);
                tp.printString(Lib.allignRight(Integer.toString(i+1), 10), colrowd, ar_col[0], p);
                tp.printVerticalLine(ar_col[1] - 1, colrowd, colrowd + 2);
                tp.printString(Lib.allignRight(Lib.NumberFormat(arrJmlObjek[i]), 25), colrowd, ar_col[1], p);
                tp.printVerticalLine(ar_col[2] - 1, colrowd, colrowd + 2);
                tp.printString(Lib.allignRight(Lib.NumberFormat(arrLuasBangunan[i]), 25), colrowd, ar_col[2], p);
                tp.printVerticalLine(ar_col[3] - 1, colrowd, colrowd + 2);
                tp.printString(Lib.allignRight(Lib.NumberFormat(arrLuasTanah[i]), 25), colrowd, ar_col[3], p);
                tp.printVerticalLine(ar_col[4] - 1, colrowd, colrowd + 2);
                tp.printString(Lib.allignRight(Lib.NumberFormat(arrPokokKetetapan[i]), 25), colrowd, ar_col[4], p);
                tp.printVerticalLine(ar_col[5] - 1, colrowd, colrowd + 2);
                jmlObjek += arrJmlObjek[i];
                jmlluasBangunan += arrLuasBangunan[i];
                jmlluasTanah += arrLuasTanah[i];
                jmlpokokKetetapan += arrPokokKetetapan[i];
                i++;  
                colrowd++;
            }
            int colrow2 = colrowd;
            tp.printHorizontalLine(colrow2, ar_col[0], ar_col[5] - 1);
            tp.printVerticalLine(ar_col[0] - 1, colrow2, colrow2 + 2);
            tp.printString("Jumlah", colrow2 + 1, ar_col[0], p);
            tp.printVerticalLine(ar_col[1] - 1, colrow2, colrow2 + 2);
            tp.printString(Lib.allignRight(Lib.NumberFormat(jmlObjek), 25), colrow2 + 1, ar_col[1], p);
            tp.printVerticalLine(ar_col[2] - 1, colrow2, colrow2 + 2);
            tp.printString(Lib.allignRight(Lib.NumberFormat(jmlluasBangunan), 25), colrow2 + 1, ar_col[2], p);
            tp.printVerticalLine(ar_col[3] - 1, colrow2, colrow2 + 2);
            tp.printString(Lib.allignRight(Lib.NumberFormat(jmlluasTanah), 25), colrow2 + 1, ar_col[3], p);
            tp.printVerticalLine(ar_col[4] - 1, colrow2, colrow2 + 2);
            tp.printString(Lib.allignRight(Lib.NumberFormat(jmlpokokKetetapan), 25), colrow2 + 1, ar_col[4], p);
            tp.printVerticalLine(ar_col[5] - 1, colrow2, colrow2 + 2);
            tp.printHorizontalLine(colrow2 + 2, ar_col[0], ar_col[5] - 1);
            tp.printString(NumberToText.convert(jmlpokokKetetapan).toUpperCase() + " RUPIAH", colrow2+3, ar_col[0], p);
            
            Date penetapan;
            DateFormat dateFormat;
            
            dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            penetapan = (Date)dateFormat.parse(tanggalPenetapan);
            tp.printString(Lib.allignCenter(namaKota+", "+Lib.dateFormatIndo2(penetapan), WIDTHRES), colrow2 + 7, 0, p);
            tp.printString(Lib.allignCenter("KEPALA DINAS PENDAPATAN DAERAH", WIDTHRES), colrow2 + 8, 0, p);
            tp.printString(Lib.allignCenter("KOTA "+ namaKota, WIDTHRES), colrow2 + 9, 0, p);
            tp.printString(Lib.allignCenter(namaPejabat, WIDTHRES), colrow2 + 14, 0, p);
            tp.printString(Lib.allignCenter(nipPejabat,WIDTHRES), colrow2 + 15, 0, p);  
        }
    }
    public void isiTotalSHI(String data[][]){
        int rowsAll = data.length;

        int banyakHalaman = rowsAll / ROWSPERPAGE;
        banyakHalaman++;
        TotalSHI = new double[banyakHalaman];
        double temp = 0;        
        
        //Loop sejumlah row
        for(int i=0;i<banyakHalaman;i++){
            for(int j=(i*ROWSPERPAGE);j<((i+1)*ROWSPERPAGE);j++){
                if(j>=rowsAll){
                    break;
                }
                temp += Double.parseDouble(data[j][10]);
            }            
            if(i==0){
                TotalSHI[i]=temp;
            }
            else{
                TotalSHI[i]=TotalSHI[i-1]+temp;
            } 
            temp = 0;
        }
    }
    public void printRealFirstPage(final MainFrame ud, final String data[][],final JProgressBar pb,final JDialog plog){
        SwingWorker letsprint = new SwingWorker<Void, Integer>(){            
            @Override
            public Void doInBackground() throws ParseException{ 
                try{
                    jp = tp.getDefaultJobProperties();
                    p = tp.getDefaultTextProperties();

                    jp.pitch = 12;
                    jp.cols = WIDTHRES;
                    jp.rows = KOLRES;
                    fp.setTimeout(100000);
                    tp.startJob(fp, jp);
                    setBlockLength(1);
                    publish(new Integer(1));
                    printFirstPage(ud,data);
                    tp.endJob();  
                }
                catch(TextPrinterException tpe){
                    Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, tpe);
                } 
                return null;
            }
            @Override
            public void done(){
                try {
                    Thread.sleep(1000);
                } 
                catch (InterruptedException ex) {
                    Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, ex);
                }
                plog.dispose();                
            }   
            @Override
            protected void process(List<Integer> chunks){
                int x = chunks.get(chunks.size()-1);
                pb.setValue(Math.round((x+1)*100/blocklength));
            }        
        };
        pb.setValue(0);
        pb.setVisible(true);        
        letsprint.execute();    
    }
    public void printRealLastPage(final MainFrame ud, final String data[][],final JProgressBar pb,final JDialog plog){   
        SwingWorker letsprint = new SwingWorker<Void, Integer>(){            
            @Override
            public Void doInBackground() throws ParseException{
                try{
                    int jumHal = (getNoHalaman(data)- 1) + 2;
                    jp = tp.getDefaultJobProperties();
                    p = tp.getDefaultTextProperties();

                    jp.pitch = 12;
                    jp.cols = WIDTHRES;
                    jp.rows = KOLRES;
                    fp.setTimeout(100000);
                    tp.startJob(fp, jp);
                    setBlockLength(jumHal);
                    publish (new Integer(jumHal));
                    printLastPage(ud,data);
                    tp.endJob();  
                }
                catch(TextPrinterException tpe){
                    Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, tpe);
                } 
                return null;
            }
            @Override
            public void done(){
                try {
                    Thread.sleep(1000);
                } 
                catch (InterruptedException ex) {
                    Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, ex);
                }
                plog.dispose();
            }   

            @Override
            protected void process(List<Integer> chunks){
                int x = chunks.get(chunks.size()-1);
                pb.setValue(Math.round((x+1)*100/blocklength));
            }
        };
        pb.setValue(0);
        pb.setVisible(true);        
        letsprint.execute();       
    }
    public void printAll(final MainFrame ud,final String data[][],final JProgressBar pb,final JDialog plog) throws ParseException{
        SwingWorker letsprint = new SwingWorker<Void, Integer>(){            
            @Override
            public Void doInBackground() throws ParseException{
                try{
                    int i,j;
                    int rowsAll = data.length;
                    int banyakHalaman = rowsAll / ROWSPERPAGE;
                    int jumHal = (getNoHalaman(data)- 1) + 2;

                    isiTotalSHI(data);
                    firstRow = 0;
                    jp = tp.getDefaultJobProperties();
                    p = tp.getDefaultTextProperties();

                    jp.pitch = 12;
                    jp.cols = WIDTHRES;
                    jp.rows = KOLRES;
                    
                    fp.setTimeout(100000);
                    tp.startJob(fp, jp);
                    setBlockLength(jumHal);
                    publish(new Integer(1));
                    printFirstPage(ud,data);
                    tp.newPage();

                    for(i = 2; i < ((banyakHalaman+1)+ 2);i++){
                        baris = 3;
                        publish (new Integer(i));
                        printHeader(ud,data,baris,i);

                        for(j = firstRow; j < (firstRow+ROWSPERPAGE); j++){      
                            if(j >= rowsAll) {
                                break;
                            }
                            baris += 3;
                            printRow(ud,data,j);

                        }
                        printFooter(baris+8, i);
                        firstRow += ROWSPERPAGE;
                        tp.newPage();     
                    }
                    publish (new Integer(jumHal));
                    printLastPage(ud,data);
                    tp.endJob();
                }
                catch(TextPrinterException tpe){
                    Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, tpe);
                }
                return null;
            }
            @Override
            public void done(){
                try {
                    Thread.sleep(1000);
                } 
                catch (InterruptedException ex) {
                    Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, ex);
                }
                plog.dispose();
        
            }   

            @Override
            protected void process(List<Integer> chunks){
                int x = chunks.get(chunks.size()-1);
                pb.setValue(Math.round((x+1)*100/blocklength));
            }
        };
        pb.setValue(0);
        pb.setVisible(true);        
        letsprint.execute();                    
    }
    public void printOnePage(final PrintDialog pd, final MainFrame ud, final String data[][], final JProgressBar pb,final JDialog plog) throws ParseException{
        SwingWorker letsprint = new SwingWorker<Void, Integer>(){            
            @Override
            public Void doInBackground() throws ParseException{
                try{
                    int halaman = Integer.parseInt(pd.getOnePage());
                    int rowsAll = data.length;
                   
                    firstRow = ROWSPERPAGE * (halaman-2);
                    isiTotalSHI(data);

                    jp = tp.getDefaultJobProperties();
                    p = tp.getDefaultTextProperties();

                    jp.pitch = 12;
                    jp.cols = WIDTHRES;
                    jp.rows = KOLRES;
                    fp.setTimeout(100000);
                    tp.startJob(fp, jp);
                    setBlockLength(halaman);

                    for(int i = halaman ; i <= halaman; i++){
                        baris = 3;
                        publish(new Integer(i));
                        printHeader(ud,data,baris,i);
                        for(int j = firstRow; j < (firstRow+ROWSPERPAGE); j++){
                            publish(new Integer(j));
                            if(j >= rowsAll) {
                                break;
                            }
                            baris += 3;
                            printRow(ud,data,j);        
                        }
                        printFooter(baris+8, i);              
                    }
                    tp.endJob();
                }
                catch(TextPrinterException tpe){
                    Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, tpe);
                }
                return null;
            }
            @Override
            public void done(){
                try {
                    Thread.sleep(1000);
                } 
                catch (InterruptedException ex) {
                    Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, ex);
                }
                plog.dispose();              
            }   

            @Override
            protected void process(List<Integer> chunks){
                int x = chunks.get(chunks.size()-1);
                pb.setValue(Math.round((x+1)*100/blocklength));
            }
        };
        pb.setValue(0);
        pb.setVisible(true);        
        letsprint.execute();           
    }
    
    public void printFromFirstPage(final PrintDialog pd, final MainFrame ud, final String data[][],final JProgressBar pb,final JDialog plog){
         SwingWorker letsprint = new SwingWorker<Void, Integer>(){            
            @Override
            public Void doInBackground() throws ParseException{
                try{
                    int i,j;
                    int rowsAll = data.length;
                    int halamanAwal = 2;
                    int halamanAkhir = Integer.parseInt(pd.getPageTo());
                    int jumHal = (halamanAkhir-halamanAwal)+2;
                    
                    isiTotalSHI(data);
                    firstRow = ROWSPERPAGE * (halamanAwal-2);
                    int endRow = (ROWSPERPAGE * halamanAkhir) - ROWSPERPAGE;
                  
                    jp = tp.getDefaultJobProperties();
                    p = tp.getDefaultTextProperties();

                    jp.pitch = 12;
                    jp.cols = WIDTHRES;
                    jp.rows = KOLRES;
                    fp.setTimeout(100000);

                    tp.startJob(fp, jp);
                    setBlockLength(jumHal);
                    publish(new Integer(1));
                    printFirstPage(ud,data);
                    tp.newPage();
                    for(i = halamanAwal ; i <= halamanAkhir; i++){
                        baris = 3;
                        publish(new Integer(i));
                        printHeader(ud,data,baris,i);
                        for(j = firstRow; j < (firstRow+ROWSPERPAGE); j++){
                            if(j >= endRow || j >= rowsAll) {
                                break;
                            }
                            baris += 3;
                            printRow(ud,data,j);

                        }
                        printFooter(baris+8, i);  
                        firstRow += ROWSPERPAGE;
                        tp.newPage();
                    }
                    tp.endJob();
                }
                catch(TextPrinterException tpe){
                    Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, tpe);
                }
                return null;
            }
            @Override
            public void done(){
                try {
                    Thread.sleep(1000);
                } 
                catch (InterruptedException ex) {
                    Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, ex);
                }
                plog.dispose();           
            }   

            @Override
            protected void process(List<Integer> chunks){
                int x = chunks.get(chunks.size()-1);
                pb.setValue(Math.round((x+1)*100/blocklength));
            }   
        };
        pb.setValue(0);
        pb.setVisible(true);        
        letsprint.execute();       
    }
    public void printToLastPage(final PrintDialog pd, final MainFrame ud, final String data[][],final JProgressBar pb,final JDialog plog){
        SwingWorker letsprint = new SwingWorker<Void, Integer>(){            
            @Override
            public Void doInBackground() throws ParseException{
                try{
                    int i,j;
                    int rowsAll = data.length;
                    int halamanAwal = Integer.parseInt(pd.getPageFrom());
                    int halamanAkhir = Integer.parseInt(pd.getPageTo()) - 1;
                    int jumHal = (halamanAkhir-halamanAwal) + 2;

                    isiTotalSHI(data);
                    firstRow = ROWSPERPAGE * (halamanAwal-2);
                    int endRow = (ROWSPERPAGE * halamanAkhir) - ROWSPERPAGE;

                    jp = tp.getDefaultJobProperties();
                    p = tp.getDefaultTextProperties();

                    jp.pitch = 12;
                    jp.cols = WIDTHRES;
                    jp.rows = KOLRES;
                    fp.setTimeout(100000);
                    tp.startJob(fp, jp);
                    setBlockLength(jumHal);

                    for(i = halamanAwal ; i <= halamanAkhir; i++){
                        baris = 3;
                        publish(new Integer(i));
                        printHeader(ud,data,baris,i);
                        for(j = firstRow; j < (firstRow+ROWSPERPAGE); j++){
                            if(j >= endRow || j >= rowsAll) {
                                break;
                            }
                            baris += 3;
                            printRow(ud,data,j);

                        }
                        printFooter(baris+8, i);  
                        firstRow += ROWSPERPAGE;
                        tp.newPage();
                    }
                    publish(new Integer(halamanAkhir+1));
                    printLastPage(ud,data);
                    tp.endJob();
                }
                catch(TextPrinterException tpe){
                    Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, tpe);   
                }
                return null;
            }
            @Override
            public void done(){
                try {
                    Thread.sleep(1000);
                } 
                catch (InterruptedException ex) {
                    Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, ex);
                }
                plog.dispose();              
            }   
            @Override
            protected void process(List<Integer> chunks){
                int x = chunks.get(chunks.size()-1);
                pb.setValue(Math.round((x+1)*100/blocklength));
            }  
        };
        pb.setValue(0);
        pb.setVisible(true);        
        letsprint.execute();    
    }
    public void printCustomPage(final PrintDialog pd, final MainFrame ud, final String data[][], final JProgressBar pb,final JDialog plog){
        SwingWorker letsprint = new SwingWorker<Void, Integer>(){            
            @Override
            public Void doInBackground() throws ParseException{  
                try{
                    int i,j;
                    int rowsAll = data.length;
                    int halamanAwal = Integer.parseInt(pd.getPageFrom());
                    int halamanAkhir = Integer.parseInt(pd.getPageTo());
                    int jumHal = (halamanAkhir-halamanAwal)+1;
                    
                    isiTotalSHI(data);
                    firstRow = ROWSPERPAGE * (halamanAwal-2);
                    int endRow = (ROWSPERPAGE * halamanAkhir) - ROWSPERPAGE;

                    jp = tp.getDefaultJobProperties();
                    p = tp.getDefaultTextProperties();

                    jp.pitch = 12;
                    jp.cols = WIDTHRES;
                    jp.rows = KOLRES;
                    fp.setTimeout(100000);
                    tp.startJob(fp, jp);
                    setBlockLength(jumHal);

                    for(i = halamanAwal ; i <= halamanAkhir; i++){
                        baris = 3;
                        publish(new Integer(i));
                        printHeader(ud,data,baris,i);
                        for(j = firstRow; j < (firstRow+ROWSPERPAGE); j++){
                            if(j >= endRow || j >= rowsAll) {
                                break;
                            }
                            baris += 3;
                            printRow(ud,data,j);
                        }
                        printFooter(baris+8, i);  
                        firstRow += ROWSPERPAGE;
                        tp.newPage();
                    }
                    tp.endJob();
                }
                catch(TextPrinterException tpe){
                     Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, tpe);
                }
                return null;
            }
            @Override
            public void done(){
                try {
                    Thread.sleep(1000);
                } 
                catch (InterruptedException ex) {
                    Logger.getLogger(Print.class.getName()).log(Level.SEVERE, null, ex);
                }
                plog.dispose();
            }   
            @Override
            protected void process(List<Integer> chunks){
                int x = chunks.get(chunks.size()-1);
                pb.setValue(Math.round((x+1)*100/blocklength));
            } 
        };
        pb.setValue(0);
        pb.setVisible(true);        
        letsprint.execute();  
    }
}
